classSolution { publicbooleanisAcronym(List<String> words, String s) { intn= words.size(), m = s.length(); if (n != m) returnfalse; for (inti=0; i < n; i++) { if (words.get(i).charAt(0) != s.charAt(i)) { returnfalse; } } returntrue; } }
C++
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { public: boolisAcronym(vector<string>& words, string s){ int m = words.size(), n = s.size(); if (m != n) returnfalse; for (int i = 0; i < n; i++) { if (words[i][0] != s[i]) { returnfalse; } } returntrue; } };
classSolution { public: intlongestEqualSubarray(vector<int>& nums, int k){ int n = nums.size(), ans = 0; vector<vector<int>> pos(n + 1); for (int i = 0; i < n; i++) { pos[nums[i]].push_back(i); } for (auto &ps : pos) { int left = 0; for (int right = 0; right < ps.size(); right++) { while (ps[right] - ps[left] - right + left > k) { left++; } ans = max(ans, right - left + 1); } } return ans; } };